namespace InventoryManagement
{
	using System;
	using System.Collections;
	using System.Collections.Generic;
	using System.ComponentModel;
	using System.Data;
	using System.Data.SqlClient;
	using System.Text;
	
	
	public class Location : BaseTable
	{
		
		#region Static SQL String Members
		/// <remarks>This field represents the full SELECT string for the table Location, with the WHERE clause.</remarks>
		internal static string _SQL_Select = "SELECT [ID], [Kode], [Nama], [Kode] + '-' + [Nama] as [Lokasi], [IsDeleted] FROM [dbo].[Location] WHERE [ID]=@ID ";
		
		/// <remarks>This field represents the full SELECT string for the table Location, with the WHERE clause.</remarks>
        internal static string _SQL_SelectByParam = @"SELECT [ID], [Kode], [Nama], [Nama], [Kode] + '-' + [Nama] as [Lokasi], [IsDeleted] FROM [dbo].[Location] WHERE isnull(convert(varchar(36),[ID]),'true')=isnull(convert(varchar(36),@ID),isnull(convert(varchar(36),[ID]),'true')) AND isnull(convert(varchar,[Kode]),'true')=isnull(convert(varchar,@Kode),isnull(convert(varchar,[Kode]),'true')) AND isnull(convert(varchar,[Nama]),'true')=isnull(convert(varchar,@Nama),isnull(convert(varchar,[Nama]),'true')) AND isnull(convert(varchar,[IsDeleted]),'true')=isnull(convert(varchar,@IsDeleted),isnull(convert(varchar,[IsDeleted]),'true')) ";
		
		/// <remarks>This field represents the full INSERT INTO string for the table Location.</remarks>
		internal static string _SQL_Insert = "INSERT INTO [dbo].[Location] ([ID], [Kode], [Nama], [IsDeleted]) VALUES(@ID, @Kode, @Nama, @IsDeleted) ";
		
		/// <remarks>This field represents the full UPDATE string for the table Location, with the WHERE clause.</remarks>
		internal static string _SQL_Update = "UPDATE [dbo].[Location] SET [ID] = @ID, [Kode] = @Kode, [Nama] = @Nama, [IsDeleted] = @IsDeleted WHERE [ID]=@ID ";
		
		/// <remarks>This field represents the DELETE string for the table Location, with the WHERE clause.</remarks>
		internal static string _SQL_Delete = "DELETE FROM [dbo].[Location] WHERE [ID]=@ID ";
		#endregion
		
		#region Tables Members
		/// <remarks>SQL Type:uniqueidentifier - </remarks>
		private System.Nullable<System.Guid> _ID;
		
		[DisplayName("ID")]
		[Category("Primary Key")]
		public System.Nullable<System.Guid> ID
		{
			get
			{
				try
				{
					return _ID;
				}
				catch (System.Exception err)
				{
					throw new Exception("Error getting ID", err);
				}
			}
			set
			{
				try
				{
					_ID = value;
				}
				catch (System.Exception err)
				{
					throw new Exception("Error setting ID", err);
				}
			}
		}
		
		/// <remarks>SQL Type:varchar - </remarks>
		private string _Kode;
		
		[DisplayName("Kode")]
		[Category("Column")]
		public string Kode
		{
			get
			{
				try
				{
					return _Kode;
				}
				catch (System.Exception err)
				{
					throw new Exception("Error getting Kode", err);
				}
			}
			set
			{
				try
				{
					if ((value.Length <= 200))
					{
						_Kode = value;
					}
					else
					{
						throw new OverflowException("Error setting Kode, Length of value is to long. Maximum Length: 200");
					}
				}
				catch (System.Exception err)
				{
					throw new Exception("Error setting Kode", err);
				}
			}
		}
		
		/// <remarks>SQL Type:varchar - </remarks>
		private string _Nama;
		
		[DisplayName("Nama")]
		[Category("Column")]
		public string Nama
		{
			get
			{
				try
				{
					return _Nama;
				}
				catch (System.Exception err)
				{
					throw new Exception("Error getting Nama", err);
				}
			}
			set
			{
				try
				{
					if ((value.Length <= 200))
					{
						_Nama = value;
					}
					else
					{
						throw new OverflowException("Error setting Nama, Length of value is to long. Maximum Length: 200");
					}
				}
				catch (System.Exception err)
				{
					throw new Exception("Error setting Nama", err);
				}
			}
		}

        /// <remarks>SQL Type:varchar - </remarks>
        private string _Lokasi;

        [DisplayName("Nama")]
        [Category("Column")]
        public string Lokasi
        {
            get
            {
                try
                {
                    return _Lokasi;
                }
                catch (System.Exception err)
                {
                    throw new Exception("Error getting Lokasi", err);
                }
            }
            set
            {
                try
                {                    
                    _Lokasi = value;
                }
                catch (System.Exception err)
                {
                    throw new Exception("Error setting Lokasi", err);
                }
            }
        }
		
		/// <remarks>SQL Type:bit - </remarks>
		private System.Nullable<bool> _IsDeleted;
		
		[DisplayName("Is Deleted")]
		[Category("Column")]
		public System.Nullable<bool> IsDeleted
		{
			get
			{
				try
				{
					return _IsDeleted;
				}
				catch (System.Exception err)
				{
					throw new Exception("Error getting IsDeleted", err);
				}
			}
			set
			{
				try
				{
					_IsDeleted = value;
				}
				catch (System.Exception err)
				{
					throw new Exception("Error setting IsDeleted", err);
				}
			}
		}
		#endregion
		
		#region Public Methods
		public void Select()
		{
			try
			{
				SqlConnection Conn = new SqlConnection(_connString);
				SqlCommand Com = Conn.CreateCommand();
				Com.CommandText = Location._SQL_Select;
				Com.Parameters.AddRange(GetSqlParameters());
				Conn.Open();
				SqlDataReader rs = Com.ExecuteReader();
				while(rs.Read())
				{
					AddFromRecordSet(rs);
				}
				rs.Close();
				Conn.Close();
				rs.Dispose();
				Com.Dispose();
				Conn.Dispose();
			}
			catch (System.Exception )
			{
				throw;
			}
		}
		
		public System.Data.DataTable SelectByParam()
		{
			try
			{
				SqlConnection Conn = new SqlConnection(_connString);
				SqlCommand Com = Conn.CreateCommand();
				Com.CommandText = Location._SQL_SelectByParam;
				Com.Parameters.AddRange(GetSqlParameters());
				Conn.Open();
				SqlDataAdapter objDataAdapter = new SqlDataAdapter(Com);
				DataTable objDataTable = new DataTable();
				objDataAdapter.Fill(objDataTable);
				Conn.Close();
				Com.Dispose();
				Conn.Dispose();
				return objDataTable;
			}
			catch (System.Exception )
			{
				throw;
			}
		}
		
		public int Insert()
		{
			try
			{
				SqlConnection Conn = new SqlConnection(_connString);
				SqlCommand Com = Conn.CreateCommand();
				Com.CommandText = Location._SQL_Insert;
				Com.Parameters.AddRange(GetSqlParameters());
				Conn.Open();
				int rowseffected = Com.ExecuteNonQuery();
				Conn.Close();
				Com.Dispose();
				Conn.Dispose();
				return rowseffected;
			}
			catch (System.Exception )
			{
				throw;
			}
		}
		
		public int Update()
		{
			try
			{
				SqlConnection Conn = new SqlConnection(_connString);
				SqlCommand Com = Conn.CreateCommand();
				Com.CommandText = Location._SQL_Update;
				Com.Parameters.AddRange(GetSqlParameters());
				Conn.Open();
				int rowseffected = Com.ExecuteNonQuery();
				Conn.Close();
				Com.Dispose();
				Conn.Dispose();
				return rowseffected;
			}
			catch (System.Exception )
			{
				throw;
			}
		}
		
		public int Delete()
		{
			try
			{
				SqlConnection Conn = new SqlConnection(_connString);
				SqlCommand Com = Conn.CreateCommand();
				Com.CommandText = Location._SQL_Delete;
				Com.Parameters.AddRange(GetSqlParameters());
				Conn.Open();
				int rowseffected = Com.ExecuteNonQuery();
				Conn.Close();
				Com.Dispose();
				Conn.Dispose();
				return rowseffected;
			}
			catch (System.Exception )
			{
				throw;
			}
		}
		
		public string[] GetSqlCommandStrings()
		{
			try
			{
				string[] CommStr = new string[4];
				CommStr[0] = "SELECT [ID], [Kode], [Nama], [IsDeleted] FROM [dbo].[Location] WHERE [ID] = " + _ID + "";
				CommStr[1] = "INSERT INTO [dbo].[Location] ([ID], [Kode], [Nama], [IsDeleted]) VALUES(" + _ID + ", '" + _Kode.Replace("'","''") + "', '" + _Nama.Replace("'","''") + "', " + _IsDeleted + ")";
				CommStr[2] = "UPDATE [dbo].[Location] SET [Kode] = '" + _Kode.Replace("'","''") + "' ,[Nama] = '" + _Nama.Replace("'","''") + "' ,[IsDeleted] = " + _IsDeleted + " WHERE [ID] = " + _ID + "";
				CommStr[3] = "DELETE FROM [dbo].[Location] WHERE [ID] = " + _ID + "";
				return CommStr;
			}
			catch (System.Exception )
			{
				throw;
			}
		}
		#endregion
		
		#region Private Methods
		private void AddFromRecordSet(SqlDataReader rs)
		{
			try
			{
				// if value from the recordset, to the ID field is NOT null then set the value.
				if ((rs.IsDBNull(rs.GetOrdinal("ID")) == false))
				{
					ID = rs.GetGuid(rs.GetOrdinal("ID"));
				}
				// if value from the recordset, to the Kode field is NOT null then set the value.
				if ((rs.IsDBNull(rs.GetOrdinal("Kode")) == false))
				{
					Kode = rs.GetString(rs.GetOrdinal("Kode"));
				}
				// if value from the recordset, to the Nama field is NOT null then set the value.
				if ((rs.IsDBNull(rs.GetOrdinal("Nama")) == false))
				{
					Nama = rs.GetString(rs.GetOrdinal("Nama"));
				}
                // if value from the recordset, to the Lokasi field is NOT null then set the value.
                if ((rs.IsDBNull(rs.GetOrdinal("Lokasi")) == false))
                {
                    Lokasi = rs.GetString(rs.GetOrdinal("Lokasi"));
                }
				// if value from the recordset, to the IsDeleted field is NOT null then set the value.
				if ((rs.IsDBNull(rs.GetOrdinal("IsDeleted")) == false))
				{
					IsDeleted = rs.GetBoolean(rs.GetOrdinal("IsDeleted"));
				}
			}
			catch (SqlException sqlExc)
			{
				throw sqlExc;
			}
			catch (Exception Exc)
			{
				throw Exc;
			}
		}
		
		private SqlParameter[] GetSqlParameters()
		{
			List<SqlParameter> SqlParmColl = new List<SqlParameter>();
			try
			{
				SqlParmColl.Add(InventoryManagement.AddSqlParm("@ID", ID, SqlDbType.UniqueIdentifier));
				SqlParmColl.Add(InventoryManagement.AddSqlParm("@Kode", Kode, SqlDbType.VarChar));
				SqlParmColl.Add(InventoryManagement.AddSqlParm("@Nama", Nama, SqlDbType.VarChar));
				SqlParmColl.Add(InventoryManagement.AddSqlParm("@IsDeleted", IsDeleted, SqlDbType.Bit));
				return SqlParmColl.ToArray();
			}
			catch (Exception Exc)
			{
				throw Exc;
			}
		}
		#endregion
	}
	
	public class LocationCollection : System.Collections.CollectionBase
	{
		
		[Description("Adds a new Location to the collection.")]
		public int Add(Location item)
		{
			int newindex = List.Add(item);
			return newindex;
		}
		
		[Description("Removes a Location from the collection.")]
		public void Remove(Location item)
		{
			List.Remove(item);
		}
		
		[Description("Inserts an Location into the collection at the specified index.")]
		public void Insert(int index, Location item)
		{
			List.Insert(index, item);
		}
		
		[Description("Returns the index value of the Location class in the collection.")]
		public int IndexOf(Location item)
		{
			return List.IndexOf(item);
		}
		
		[Description("Returns true if the Location class is present in the collection.")]
		public bool Contains(Location item)
		{
			return List.Contains(item);
		}
	}
	
	public enum Location_Fields
	{
		
		ID,
		
		Kode,
		
		Nama,

        Lokasi,
		
		IsDeleted,
	}
}
